package de.baumann.browser.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

import de.baumann.browser.OdinApplication;

/**
*@author:
*@date:2019/7/15 15:16
*@discription: 数据缓存工具类
*/
public class InternalStorageUtil {

    private Context context;

    private static ConcurrentHashMap<String, Object> localData = new ConcurrentHashMap<>();

    @SuppressLint("StaticFieldLeak")
    private static InternalStorageUtil internalStorage = null;

    private final String localDirName = "odinStorage";

    private InternalStorageUtil(Context context) {
        this.context = context;
    }

    public static InternalStorageUtil getInstance() {
        if (internalStorage == null) {
            internalStorage = new InternalStorageUtil(OdinApplication.getInstance());
        }
        return internalStorage;
    }

    public boolean save(String name, String value) {
        if (name == null || "".equals(name.trim())) {
            return false;
        }
        FileWriter fw = null;
        try {
            File file = new File(context.getDir(localDirName, Context.MODE_PRIVATE), name);
            if (file.exists() || file.createNewFile()) {
                fw = new FileWriter(file);
                fw.write(value);
                localData.put(name, value);
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean save(String name, Serializable serializable) {
        if (name == null || "".equals(name.trim())) {
            return false;
        }
        try {
            File file = new File(context.getDir(localDirName, Context.MODE_PRIVATE), name);
            if (file.exists() || file.createNewFile()) {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(serializable);
                localData.put(name, serializable);
                oos.close();
                return true;
            } else {
                return false;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public String get(String name) {
        if (name == null || "".equals(name.trim())) {
            return null;
        }
        if (localData.containsKey(name)) {
            Object o = localData.get(name);
            if (o != null && !(o instanceof String)) {
                return o.toString();
            }
            return (String) o;
        }
        File file = new File(context.getDir(localDirName, Context.MODE_PRIVATE), name);
        if (file.exists()) {
            FileReader fr = null;
            try {
                fr = new FileReader(file);
                if (fr.ready()) {
                    char[] data = new char[128];
                    int len = 128;
                    StringBuilder sb = new StringBuilder();
                    while ((len = fr.read(data, 0, len)) != -1) {
                        sb.append(data);
                    }
                    String value = sb.toString();
                    localData.put(name, value);
                    return value;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fr != null) {
                        fr.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public Serializable getSerializable(String name) {
        if (name == null || "".equals(name.trim())) {
            return null;
        }
        if (localData.containsKey(name)) {
            Object o = localData.get(name);
            return (Serializable) o;
        }
        File file = new File(context.getDir(localDirName, Context.MODE_PRIVATE), name);
        if (file.exists()) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(file));
                return (Serializable) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private boolean delete(String name) {
        if (name != null && !"".equals(name.trim())) {
            File file = new File(context.getDir(localDirName, Context.MODE_PRIVATE), name);
            if (file.exists() && file.delete()) {
                localData.remove(name);
                return true;
            }
        }
        return false;
    }
}
